﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Data;
using DreamBuilder.DictionaryAccess;

namespace DreamBuilder.UI.UserControls.DataAccess.Services
{
    public abstract class RefTreeDAL : UserControlsDataAccessBase
    {
        public IDataReader GetTreeData(TableDicInfo TableInfo)
        {
            string sSql = "";
            try
            {
                if (TableInfo != null)
                {
                    if (TableInfo.ParentField.Trim() != "")
                    {
                        sSql = "select  " + TableInfo.PrimaryField + "," + TableInfo.DisplayField + "," + TableInfo.ParentField + " from " +
                            TableInfo.TableCode;
                    }
                    else
                    {
                        sSql = "select " + TableInfo.PrimaryField + "," + TableInfo.DisplayField + " from " + TableInfo.TableCode;
                    }
                    return DBHelper.ExecuteReader(sSql);
                }
                else
                {
                    return null;
                }
            }
            catch (System.Exception err)
            {
                return null;
            }
        }


        public IDataReader GetParentTreeData(string Id, string sNumber, TableDicInfo pTableInfo)
        {
            string sSql = "";
            if (Id.Trim() == "-1")
            {
                sSql = "select " + pTableInfo.PrimaryField + "," + pTableInfo.DisplayField + " from " + pTableInfo.TableCode + " where " + pTableInfo.ParentField + " = (select " + pTableInfo.PrimaryField + "  from " + pTableInfo.TableCode + " where " + pTableInfo.ParentField + " is null )";
            }
            else
            {
                sSql = "select " + pTableInfo.PrimaryField + "," + pTableInfo.DisplayField + " from " + pTableInfo.TableCode + " where " + pTableInfo.ParentField + " = '" + sNumber + "'";
            }
            return DBHelper.ExecuteReader(sSql);
        }
    }
}
